import {Injectable} from '@angular/core';
import 'rxjs/add/operator/toPromise';
import {HttpClient, HttpHeaders, HttpResponse} from '@angular/common/http';

@Injectable()
export class HttpBaseService {
  header = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});
  options = {headers: this.header, withCredentials: true, params: null};

  constructor(private httpClient: HttpClient) {
  }

  /**
   * 统一发送请求(入口方法)
   * @param params
   * @returns {Promise<{success: boolean, msg: string}>|Promise<R>}
   */
  public request(params: any): any {
    if (params['method'] === 'get' || params['method'] === 'GET') {
      return this.get(params['url'], params['data']);
    } else if (params['method'] === 'post' || params['method'] === 'POST') {
      return this.post(params['url'], params['data']);
    } else if (params['method'] === 'delete' || params['method'] === 'DELETE') {
      return this.delete(params['url'], params['data']);
    } else if (params['method'] === 'put' || params['method'] === 'PUT') {
      return this.put(params['url'], params['data']);
    }
  }

  /**
    * get请求 （获取数据）
    * @param url 接口地址
    * @param params 参数
    * @returns {Promise<R>|Promise<U>}
    */
  public get(url: string, params: any): any {
    this.options.params = params;
    return this.httpClient.get(url, this.options)
      .toPromise()
      .then(this.handleSuccess)
      .catch(res => this.handleError(res));
  }

  /**
    * post请求 （添加数据）
    * @param url 接口地址
    * @param params 参数
    * @returns {Promise<R>|Promise<U>}
    */
  public post(url: string, params: any) {
    return this.httpClient.post(url, params, this.options)
      .toPromise()
      .then(this.handleSuccess)
      .catch(res => this.handleError(res));
  }

  /**
    * delete请求 （删除数据）
    * @param url 接口地址
    * @param params 参数
    * @returns {Promise<R>|Promise<U>}
    */
  public delete(url: string, params: any) {
    this.options.params = params;
    return this.httpClient.delete(url, this.options)
      .toPromise()
      .then(this.handleSuccess)
      .catch(res => this.handleError(res));
  }

  /**
    * put请求 （更新数据）
    * @param url 接口地址
    * @param params 参数
    * @returns {Promise<R>|Promise<U>}
    */
  public put(url: string, params: any) {
    return this.httpClient.put(url, params, this.options)
      .toPromise()
      .then(this.handleSuccess)
      .catch(res => this.handleError(res));
  }

  /**
    * 处理请求成功
    * @param res
    * @returns {{data: (string|null|((node:any)=>any)
 */
  private handleSuccess(res: HttpResponse<any>) {
    console.log(res);
    return res;
  }

  /**
    * 处理请求错误
    * @param error
    * @returns {void|Promise<string>|Promise<T>|any}
    */
  private handleError(error) {
    const msg = '请求失败';
    if (error.status === 400) {
      console.log('请求参数正确');
    }
    if (error.status === 404) {
      console.error('请检查路径是否正确');
    }
    if (error.status === 500) {
      console.error('请求的服务器错误');
    }
    return {success: false, msg: msg};

  }
}
